<?php

namespace App\Service\playlet;
use App\Model\BaseBanner;
use App\Model\PlayletBlocksRanking;
use App\Service\CommicService;
use App\Model\Company;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
use Hyperf\DbConnection\Db;

class BaseBannerService extends CommicService
{
    const cacheDuration = 60 * 15;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }


    const STATUS_ENABLE  = 1;


    public function search(Array $params){

        $keyword     = $params['keyword'] ?? '';
        $status      = $params['status'] ?? '';
        $pageSize    = $params['pageSize'] ?? 10;
        $page        = $params['page'] ?? 1;

        $query = BaseBanner::query()->select(['id','name','cover','status','sort','created_at','updated_at']);

        if ($keyword) {
            $query->orWhere([['name', 'like', '%'.$keyword.'%']]);
        }
        if ($status != '') {
            $query->where(['status' => $status]);
        }
        $query->where(['deleted' => 0]);
        $models = $query->where(['template_type' => '101'])->orderBy('updated_at','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();

            if(!empty($item->applet)){
                $applet_name = $item->applet->name;
            }else{
                $applet_name = '公共配置';
            }

            $data['applet_name']  = $applet_name;
            $data['status_name']  = self::$commonStatus[$item->status];
            $selectData[] =$data;

        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }

    public function createOrUpdate(Array $postData,$template_type=101,BaseBanner $baseBannerInfo = null){
        $name = $postData['name'];
        $status = $postData['status'];
        $cover = $postData['cover'];
        $sort = $postData['sort'];
        Db::beginTransaction();
        try {

            $model = !empty($baseBannerInfo)? $baseBannerInfo : new BaseBanner();
            $model->name = $name;
            $model->status = $status;
            $model->cover = $cover;
            $model->sort = $sort;
            $model->template_type = $template_type;
            if($model->save()){

                //查询ranking表中的banner是否有同样的图
                $rankingData = PlayletBlocksRanking::query()->where(['template_type' => $template_type])->first();

                $playletIds = explode(',',$rankingData->playlet_ids);
                if(!in_array($model->id,$playletIds)){
                    $playletIds[] = $model->id;
                    $rankingData->playlet_ids = implode(',',$playletIds);
                    $rankingData->save();
                }

                Db::commit();
                return $this->response(SystemCode::SYSTEM_SUCCESS);
            }else{
                Db::rollBack();
                return $this->response(SystemCode::SYSTEM_ERROR,'保存错误');
            }

        }catch (\Exception $e){
            Db::rollBack();
            return $this->response(SystemCode::SYSTEM_ERROR,$e->getMessage());
        }

    }
}